﻿#include "measure_size.h"
#include "qdebug.h"
#include "QTime"
#include "QDir"
Measure_Size::Measure_Size()
{

}

//0 ok 1尺寸不合格
int Measure_Size::M_Length(HObject img,bool saveImg,HTuple &len)
{
    
    int flag=0;
    len=0;
    M_Length_Index++;
    QString OK_Path=QDir::currentPath()+"/Pic/ccd2/OK/"+QString::number(M_Length_Index)+".bmp";
    QString NG_Path=QDir::currentPath()+"/Pic/ccd2/NG/"+QString::number(M_Length_Index)+".bmp";
    QString Abnormal_Path=QDir::currentPath()+"/Pic/ccd2/Abnormal/"+QString::number(M_Length_Index)+".bmp";

    //存图 0 不存  1 存图
    bool bSaveImage = bool(para2.Save_Image);
    bSaveImage=false;
    try
    {
        HObject  ROI;
        HObject  ImageReduced, Region;
        HTuple  Index, Area, Row, Column;
        HTuple  Height, Row1, Row2, Column1, Column2;
        HTuple  Row11, Row21, Column11, Column21, MetrologyHandle1;
        HTuple  line1, MetrologyHandle2 ;
		HTuple  line2, Distance, Num1,Num2;
        float  Length;

        //************根据不同型号的产品，参数也不一致

        ////换算比例=（实际尺寸/像素大小）
        //float Scale=0.004885657;
        ////标准尺寸
        //float Standard=7;
        ////尺寸允许偏差
        //float Deviation=0.04;
        ////上边与下边的间距
        //float Gap_Point=1800;
        ////列/横坐标是固定的，根据相机的焦平面确定，相机不动，此参数不动
        //float Glass_H=1550;
        ////测量边的长度
        //int Mes_Length=80;
        //

        
        //换算比例=（实际尺寸/像素大小）
        float Scale = para2.Scale;
        //标准尺寸
        float Standard = para2.Standard;
        //尺寸补偿值
        float OffSet = para2.OffSet;
        //尺寸允许上限偏差
        float Up_Deviation = para2.Up_Deviation;
        //尺寸允许下限偏差
        float Down_Deviation = para2.Down_Deviation;
        //产品上端坐标x
        float Product_X = para2.Gap_Point;
        //产品上端坐标y
        float Product_Y = para2.Glass_H;
        //测量边的长度
        int Mes_Length = para2.Mes_Length;
        //
        HObject ho_ROI_0,ho_ImageReduced,ho_Region,ho_RegionTrans;
        HTuple hv_Row1, hv_Column1, hv_Row2,hv_Column2;
        HObject ROI_1,ROI_2,ImageReduced1,ImageReduced2,Region1,Region2,ConnectedRegion1,ConnectedRegion2,SelectedRegion1,SelectedRegion2;
        HObject RegionTrans1,RegionTrans2;
        HTuple Row1_P,H1,Col2_P,W1,W2,H2,Phi1,Phi2,Col2_G,Col1_G,Row2_G,Row1_G,C1,R1,C2,R2,Row2_P,Col1_P;
        //定位产品
//		GenRectangle1(&ROI_1, 179.339, 801.853, 645.949, 1565.38);
        GenRectangle1(&ROI_1,  m_placeA.x1,  m_placeA.y1, m_placeA.x2, m_placeA.y2);

        ReduceDomain(img, ROI_1, &ImageReduced1);
        Threshold(ImageReduced1, &Region1, 0, 128);
        Connection (Region1, &ConnectedRegion1);
		SelectShape(ConnectedRegion1, &ConnectedRegion1, "area", "and", HTuple(1000), HTuple(900000));
        SelectShapeStd(ConnectedRegion1, &SelectedRegion1, "max_area", 70);
		CountObj(SelectedRegion1, &Num1);
	
		if (Num1.D() != 1)
		{
			len = 0;
			return 1;
		}
        ShapeTrans(SelectedRegion1, &RegionTrans1, "rectangle1");
        SmallestRectangle2(RegionTrans1, &R1, &C1, &Phi1, &W1,&H1);
		
        Row1_P=R1-H1;
        Row2_P=Row1_P;
        Col1_P=C1-W1/2;
        Col2_P=C1+W1/2;

        //定位玻璃
//		GenRectangle1(&ROI_2, 1690.96, 1661.15, 1870.8, 1953.62);
        GenRectangle1(&ROI_2,  m_placeB.x1,  m_placeB.y1, m_placeB.x2, m_placeB.y2);

        ReduceDomain(img, ROI_2, &ImageReduced2);
        Threshold(ImageReduced2, &Region2, 0, 128);
        Connection (Region2, &ConnectedRegion2);
		SelectShape(ConnectedRegion2, &ConnectedRegion2, "area", "and", HTuple(1000), HTuple(900000));
        SelectShapeStd(ConnectedRegion2, &SelectedRegion2, "max_area", 70);
		CountObj(SelectedRegion2, &Num2);
		if (Num2.D() != 1)
		{
			len = 0;
			return 1;
		}
        ShapeTrans(SelectedRegion2, &RegionTrans2, "rectangle1");
        SmallestRectangle2(RegionTrans2, &R2, &C2, &Phi2, &W2,&H2);
	
        Row1_G=R2-H2;
        Row2_G=Row1_G;
        Col1_G=C2-W2/2;
        Col2_G=C2+W2/2;

        //确定玻璃边缘得坐标
    /*
        //GenRectangle1(&ho_ROI_0, 1659.96, 1421.59, 1832.63, 1782.53);
		GenRectangle1(&ho_ROI_0, 1085, 541, 1358, 1984);
        ReduceDomain(img, ho_ROI_0, &ho_ImageReduced);
        Threshold(ho_ImageReduced, &ho_Region, 128, 255);
        ShapeTrans(ho_Region, &ho_RegionTrans, "rectangle1");
        SmallestRectangle1(ho_RegionTrans, &hv_Row1, &hv_Column1, &hv_Row2, &hv_Column2);


        //******************************
        // GenRectangle1(&ROI, 125.992, 83.7188, 587.789, 1306.09);
        // GenRectangle1(&ROI, 125.992, 83.7188, 900, 1306.09);
        // gen_rectangle2 (Rectangle, 464, 695, 0, 900, 200)
        //GenRectangle2(&ROI, Product_X, Product_Y, 0, 900, 200);
		GenRectangle1(&ROI, 1671.52, 2108.91, 1875.66, 2331.5);
		
        //327 695
        ReduceDomain(img, ROI, &ImageReduced);
        Threshold(ImageReduced, &Region, 0, 128);
        Connection (Region, &Region);
        SelectShape (Region, &Region, "area", "and", 10000, 99999000000);
        AreaCenter(Region, &Area, &Row, &Column);
        RegionFeatures(Region, "height", &Height);
        Row = Row-(Height/2);
        Row1 = Row-15;
        Row2 = Row-15;
        Column1 = Column-Mes_Length;
        Column2 = Column+Mes_Length;
        //        Row11 = Gap_Point;
        //        Row21 = Gap_Point;
        Row11 = hv_Row1;
        Row21 = hv_Row1;
        //        Column11 = Glass_H-Mes_Length;
        //        Column21 = Glass_H+Mes_Length;
        Column11 = 1550-Mes_Length;
        Column21 = 1550+Mes_Length;
*/
        //上半部分
        CreateMetrologyModel(&MetrologyHandle1);
        AddMetrologyObjectLineMeasure(MetrologyHandle1, Row1_P, Col1_P, Row2_P,
                                      Col2_P, 20, 5, 1, 30, HTuple(), HTuple(), &Index);
        ApplyMetrologyModel(img, MetrologyHandle1);
        GetMetrologyObjectResult(MetrologyHandle1, "all", "all", "result_type", "all_param",
                                 &line1);
        ClearMetrologyModel(MetrologyHandle1);
        //下半部分
        CreateMetrologyModel(&MetrologyHandle2);
        AddMetrologyObjectLineMeasure(MetrologyHandle2, Row1_G, Col1_G, Row2_G,
                                      Col2_G, 20, 5, 1, 30, HTuple(), HTuple(), &Index);
        ApplyMetrologyModel(img, MetrologyHandle2);
        GetMetrologyObjectResult(MetrologyHandle2, "all", "all", "result_type", "all_param",
                                 &line2);
        ClearMetrologyModel(MetrologyHandle2);

        if(line1.D()!=0&&line2.D()!=0)
        {
            Distance=(line2[0]+line2[2])/2-(line1[0]+line1[2])/2;
            Length = Scale*Distance+OffSet;
            len = Length;
            //            TupleString(len,".4f",&len);
            //            len = len.TupleString(".5");
            if(!((Length<(Standard+Up_Deviation))&&(Length>(Standard-Down_Deviation))))
            {
                flag=1;
            }
//            if(bSaveImage)
//            {
//                if(flag==1)
//                {
//                    WriteImage(img,"bmp", 0, NG_Path.toStdString().c_str());
//                }
//                else
//                {
//                    if(m_nSaveOkImage){
//                         WriteImage(img,"bmp", 0, OK_Path.toStdString().c_str());
//                    }

//                }
//            }

            return flag;
        }
        else
        {
            len=0;
//            if(bSaveImage)
//            {
//                WriteImage(img,"bmp", 0, NG_Path.toStdString().c_str());
//            }
            return 1;
        }

    }
    catch(HException &exception)
    {
        len=0;
//        if(bSaveImage)
//        {
//            WriteImage(img,"bmp", 0, Abnormal_Path.toStdString().c_str());
//        }

        return 1;
    }
}



